﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SerializedData;
using KnowledgeBase;
using KnowledgeBaseIntermediary;


namespace TestProject
{
    /// <summary>
    /// Summary description for CheckSaving
    /// </summary>
    [TestClass]
    public class CheckSaving
    {
        public CheckSaving()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void checkSavingCommand()
        {
            DataCollection dc = DataCollection.NewInstance;
            SCommand testCommand = new SCommand(dc.getCommandInt(), "testcommand", "testdescription", "testtts", "testudp");
            dc.addCommand(testCommand);
            dc.saveChanges();

            dc = null;
            dc = DataCollection.NewInstance;

            Assert.IsTrue(dc.getCommandPool().Find(o => (o.getId()).Equals(testCommand.getId())) != null);

        }

        [TestMethod]
        public void checkSavingSensor()
        {
            DataCollection dc = DataCollection.NewInstance;
            SSensor sensor=new SSensor(dc.getSensorInt(),"testsensor","testudp","testdescription");
            dc.modifyOrAddSensor(sensor);
            dc.saveChanges();

            dc = null;
            dc = DataCollection.NewInstance;

            Assert.IsTrue(dc.getSensorPool().Find(o => (o.getId()).Equals(sensor.getId())) != null); 


        }

        [TestMethod]
        public void checkSavingScene()
        {
            DataCollection dc = DataCollection.NewInstance;
            SScene scene = new SScene(dc.getSceneInt(), "test", "sdasd", "sadasd");
            dc.modifyOrAddScene(scene);
            dc.saveChanges();

            dc = null;
            dc = DataCollection.NewInstance;

            Assert.IsTrue(dc.getScenePool().Find(o => (o.getId()).Equals(scene.getId())) != null);

            //IKnowledgeBase kb = new KnowledgeBaseImpl();


        }

        [TestMethod]
        public void checkSavingState()
        {
            DataCollection dc = DataCollection.NewInstance;
            SState state = new SState(dc.getStateInt(), "sds", "sdsd", "sdsd", false, StateDTO.PRIORITY.HIGH);
            dc.modifyOrAddState(state);
            dc.saveChanges();
            
            dc = null;
            dc = DataCollection.NewInstance;
            
            Assert.IsTrue(dc.getStatePool().Find(o => (o.getId()).Equals(state.getId()))!=null);
            

            //IKnowledgeBase kb = new KnowledgeBaseImpl();


        }

        [TestMethod]
        public void checkGetDTOByEvent()
        {
            DataCollection dc = DataCollection.NewInstance;

            SCommand autoCommand = new SCommand(dc.getCommandInt(), "Auto Command Name", "Auto Command Description", "Auto Command TTS", "Auto Command UDP");
            SCommand manualCommand = new SCommand(dc.getCommandInt(), "Manual Command Name", "Manual Command Description", "Manual Command TTS", "Manual Command UDP");
            SCommand forSceneCommand = new SCommand(dc.getCommandInt(), "Scene Command Name", "Scene Command Description", "Scene Command TTS", "Scene Command UDP");
            dc.addCommand(autoCommand);
            dc.addCommand(manualCommand);
            dc.addCommand(forSceneCommand);


            SScene scene = new SScene(dc.getSceneInt(), "Test Scene", "Test Scene Description", "Test Scene TTS");
            dc.modifyOrAddScene(scene);

            SState state = new SState(dc.getStateInt(), "Test State", "Test State", "Test State", false, StateDTO.PRIORITY.HIGH);
            dc.modifyOrAddState(state);

            state.addAutoCommand(autoCommand);
            state.addManualCommandById(""+manualCommand.getId());
            scene.addCommand(forSceneCommand);
            state.addScene(scene);

            

            SSensor sensor = new SSensor(dc.getSensorInt(), "Testsensor", "Sensor UDP", "Test Sensor description");
            sensor.setRefState(state.getId());
            dc.modifyOrAddSensor(sensor);

            dc.saveChanges();


            IKnowledgeBase kb = new KnowledgeBaseImpl();
            StateDTO dto= kb.getDTOByEvent(sensor.getUdp());

            Assert.AreEqual(state.getName(), dto.Name);
            Assert.AreEqual(state.getPriority(), dto.Priority);
            Assert.AreEqual(state.getTts(), dto.OutputObject.getTtsText());
            Assert.AreEqual(state.getDescription(), dto.OutputObject.getDescription());

            Assert.AreEqual(dto.getAutoCommands().ToArray()[0],(autoCommand.getName()));
            Assert.AreEqual(dto.getManualCommands().ToArray()[0], (manualCommand.getName()));
            Assert.AreEqual(dto.getScenes().ToArray()[0], (scene.getName()));
        }

        [TestMethod]
        public void checkGetDTOByCommand()
        {
            DataCollection dc = DataCollection.NewInstance;

            SCommand autoCommand = new SCommand(dc.getCommandInt(), "Auto Command Name", "Auto Command Description", "Auto Command TTS", "Auto Command UDP");
            SCommand manualCommand = new SCommand(dc.getCommandInt(), "Manual Command Name", "Manual Command Description", "Manual Command TTS", "Manual Command UDP");
            SCommand forSceneCommand = new SCommand(dc.getCommandInt(), "Scene Command Name", "Scene Command Description", "Scene Command TTS", "Scene Command UDP");
            dc.addCommand(autoCommand);
            dc.addCommand(manualCommand);
            dc.addCommand(forSceneCommand);


            SScene scene = new SScene(dc.getSceneInt(), "Test Scene", "Test Scene Description", "Test Scene TTS");
            dc.modifyOrAddScene(scene);

            SState state = new SState(dc.getStateInt(), "Test State Name", "Description", "TTS text", false, StateDTO.PRIORITY.HIGH);
            dc.modifyOrAddState(state);

            state.addAutoCommand(autoCommand);
            state.addManualCommand(manualCommand);
            scene.addCommand(forSceneCommand);
            state.addScene(scene);



            SSensor sensor = new SSensor(dc.getSensorInt(), "Testsensor", "Sensor UDP", "Test Sensor description");
            sensor.setRefState(state.getId());
            dc.modifyOrAddSensor(sensor);

            dc.saveChanges();


            IKnowledgeBase kb = new KnowledgeBaseImpl();
            StateDTO dto = kb.getDTOByCommand("Test State Name");

            Assert.AreEqual(state.getName(), dto.Name);
            Assert.AreEqual(state.getPriority(), dto.Priority);
            Assert.AreEqual(state.getTts(), dto.OutputObject.getTtsText());
            Assert.AreEqual(state.getDescription(), dto.OutputObject.getDescription());

            Assert.AreEqual(dto.getAutoCommands().ToArray()[0], (autoCommand.getName()));
            Assert.AreEqual(dto.getManualCommands().ToArray()[0], (manualCommand.getName()));
            Assert.AreEqual(dto.getScenes().ToArray()[0], (scene.getName()));
        }

        [TestMethod]
        public void checkGetUDPByCommand()
        {
            DataCollection dc = DataCollection.NewInstance;

            SCommand autoCommand = new SCommand(dc.getCommandInt(), "Auto Command Name", "Auto Command Description", "Auto Command TTS", "Auto Command UDP");
            SCommand manualCommand = new SCommand(dc.getCommandInt(), "Manual Command Name", "Manual Command Description", "Manual Command TTS", "Manual Command UDP");
            SCommand forSceneCommand = new SCommand(dc.getCommandInt(), "Scene Command Name", "Scene Command Description", "Scene Command TTS", "Scene Command UDP");
            dc.addCommand(autoCommand);
            dc.addCommand(manualCommand);
            dc.addCommand(forSceneCommand);


            SScene scene = new SScene(dc.getSceneInt(), "Test Scene", "Test Scene Description", "Test Scene TTS");
            dc.modifyOrAddScene(scene);

            SState state = new SState(dc.getStateInt(), "Test State Name", "Description", "TTS text", false, StateDTO.PRIORITY.HIGH);
            dc.modifyOrAddState(state);

            state.addAutoCommand(autoCommand);
            state.addManualCommand(manualCommand);
            scene.addCommand(forSceneCommand);
            state.addScene(scene);



            SSensor sensor = new SSensor(dc.getSensorInt(), "Testsensor", "Sensor UDP", "Test Sensor description");
            sensor.setRefState(state.getId());
            dc.modifyOrAddSensor(sensor);

            dc.saveChanges();


            IKnowledgeBase kb = new KnowledgeBaseImpl();
            CommandObject udp = kb.getUDPByCommand(state.getName(), manualCommand.getName());

            Assert.AreEqual(manualCommand.getUDP(), udp.getUdpString());

            Assert.AreEqual(manualCommand.getTts(), udp.getOutputObject().getTtsText());

        }

    }
}
